package com.hy.demo.ui;

import android.content.pm.ActivityInfo;
import android.util.SparseArray;
import android.view.MenuItem;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.hy.demo.app.BaseActivity;
import com.hy.demo.R;
import com.hy.demo.ui.fragment.HomeFragment;
import com.hy.demo.ui.fragment.MsgFragment;
import com.hy.frame.util.LogUtil;

/**
 * title MainActivity
 * author heyan
 * time 19-7-11 下午3:27
 * desc 无
 */
public class MainActivity extends BaseActivity {
    private BottomNavigationView tabFoot;
    private SparseArray<Fragment> mFragments;

    @Override
    protected int getScreenOrientation() {
        return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
    }

    @Override
    protected boolean isSingleLayout() {
        return true;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.v_main;
    }

    @Override
    protected void initView() {
        tabFoot = findViewById(R.id.main_tabFoot);
    }

    @Override
    protected void initData() {
        setTitle(R.string.appName);
        tabFoot.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                LogUtil.d(MainActivity.this.getClass(), "menu=" + item.getTitle());
                if (tabFoot.getSelectedItemId() == item.getItemId()) return false;//重复点击 不再响应
                Fragment from = mFragments.get(tabFoot.getSelectedItemId());
                Fragment to = mFragments.get(item.getItemId());
                if (to == null) return false;
                changePager(from, to);
                return true;
            }
        });
        mFragments = new SparseArray<>();
        mFragments.append(R.id.tabHome, HomeFragment.newInstance(this));
        mFragments.append(R.id.tabMsg, MsgFragment.newInstance(this));
        changePager(null, mFragments.get(R.id.tabHome));
    }

    @Override
    public void onViewClick(View v) {

    }

    @Override
    public void onRightClick() {

    }

    @Override
    public void onBackPressed() {
        if (getCurApp() != null)
            getCurApp().exit();
    }

    private void changePager(Fragment from, Fragment to) {
        if (to == null) return;
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (from != null) { //((IBaseFragment) from.fragment).onIPause();
            transaction.detach(from); //分离指定Fragment的UI视图
        }
        boolean needAdd = false;
        if (to.isDetached()) { //目标Fragment已经添加
            transaction.attach(to); // 重新关联一个Fragment
        } else { //目标Fragment没有添加
            needAdd = true;
            transaction.add(R.id.container, to, to.getClass().getSimpleName());
        }
        //执行
        transaction.commitAllowingStateLoss();
        if (!needAdd) {
//            to.onRestart();
        }
    }
}
